********************************************************************************
use ${orig}/beforeseasonaladjustment_1.dta, clear
append using ${orig}/beforeseasonaladjustment_2.dta
append using ${orig}/beforeseasonaladjustment_3.dta
append using ${orig}/beforeseasonaladjustment_4.dta

drop if id_geo==.
egen id_time=group(eventdate) 
preserve
   keep id_time eventdate
   duplicates drop
   save "$data_proc/id_time.dta", replace
restore
 
drop eventdate

tab w93_3
********************************************************************************
* Drop public sector & education 
drop if w93_3>=750 & w93_3<900
********************************************************************************
* Drop agriculture and mining:
drop if w93_3<150
********************************************************************************
egen id_ind=group(w93_3)
preserve
keep id_ind w93_3
duplicates drop
save "$data_proc/industry_clas.dta", replace
restore
drop w93_3
save "$data_proc/temp.dta", replace
********************************************************************************
use "$data_proc/temp.dta", clear
qui: sum id_geo, detail
qui: global maxI=`r(max)' 
qui: sum id_ind, detail
qui: local maxj=`r(max)' 
qui: sum id_ind, detail
qui: local minj=`r(min)' 
forvalues i=1(1)$maxI{
qui: use "$data_proc/temp.dta", clear
qui: keep if id_geo==`i'
qui: reshape wide employment, i(id_time) j(id_ind)
qui: save "$data_proc/temp`i'.dta", replace
}
qui: use "$data_proc/temp1.dta", clear
qui: capture erase "$data_proc/temp1.dta"
forvalues i=2(1)$maxI{
qui: append using "$data_proc/temp`i'.dta"
qui: capture erase "$data_proc/temp`i'.dta"
}
********************************************************************************
forvalues j=`minj'(1)`maxj'{
qui: replace employment`j'=0 if employment`j'==.
}
save "$data_proc/temp.dta", replace 
use "$data_proc/temp.dta", clear
forvalues i=1(1)$maxI{
qui: use "$data_proc/temp.dta", clear
qui: keep if id_geo==`i'
qui: reshape long employment, i(id_time) j(id_ind)
qui: save "$data_proc/temp`i'.dta"
}
qui: capture erase "$data_proc/temp.dta"
 
qui: use "$data_proc/temp1.dta", clear
qui: capture erase "$data_proc/temp1.dta"
forvalues i=2(1)$maxI{
qui: append using "$data_proc/temp`i'.dta"
qui: capture erase "$data_proc/temp`i'.dta"
}
save "$data_proc/employment_nomissing.dta", replace 
********************************************************************************
use "$data_proc/employment_nomissing.dta", clear
********************************************************************************
qui: merge m:1 id_ind using "$data_proc/industry_clas.dta"
qui: drop if _merge!=3
qui: drop _merge
qui: tostring w93_3, force replace
qui: gen len=length(w93_3)
tab len
qui: drop len
qui: gen w93_3_2=substr(w93_3,1,2)
qui: destring w93_3_2, force replace
qui: by id_time id_geo w93_3_2, sort: egen empl_jkt=total(employment)
********************************************************************************
keep empl_jkt id_time w93_3_2 id_geo
duplicates drop
egen id_ind_2=group(w93_3_2) 
drop w93_3_2
sum id_ind_2, detail
global trad_min1=`r(min)'
sum id_ind_2, detail
global trad_max1=`r(max)'
merge m:1 id_time using "$data_proc/id_time.dta"
drop if _merge!=3
drop _merge

replace empl_jkt=0 if empl_jkt==.      

merge m:1 id_geo using id_district.dta
drop if _merge!=3
drop _merge

ren district krs
merge m:1 krs using amr_krs_72.dta
drop if _merge==1
drop _merge

collapse (sum) empl_jkt, by(amr eventdate)

* adjustment for amr==50:
qui: gen impute = 1 if amr==50 & eventdate>=156 & eventdate<160

qui: gen aux = ln(empl_jkt)
qui: xtset amr eventdate
qui: gen change = (1+aux-l5.aux)^(1/5)-1
qui: replace  aux = l.aux+f4.change if eventdate==156
qui: replace  aux = l.aux+f3.change if eventdate==157
qui: replace  aux = l.aux+f2.change if eventdate==158
qui: replace  aux = l.aux+f1.change if eventdate==159
qui: replace empl_jkt = exp(aux) if impute==1
qui: drop impute change aux

sum amr
global N = `r(max)'
reshape wide empl_jkt, i(eventdate) j(amr)
tsset eventdate
forv n = 1(1)$N {
di `n'
seasonallyadjust empl_jkt`n', gen(empl_jkt_sa`n') ar(1) ma(1) x11 multiplicative replace
}
reshape long empl_jkt empl_jkt_sa, i(eventdate) j(amr)
egen idtime=group(eventdate)
ren empl_jkt employment
ren empl_jkt_sa employment_sa
merge m:1 idtime using oecd_dates.dta
drop _merge
keep amr eventdate employment employment_sa
save ${orig}/employmentwidedata.dta, replace
********************************************************************************
